package app.server.s04;

import app.model.AppBasedoc;
import app.model.AppMessage;

import com.agile.jfinal.controller.CoreController;
import com.jfinal.ext.render.DwzRender;
import com.lasun.server.MessageServer;

/**
 * APP消息推送
 * @author zhangxiaoping  2014年04月01日
 */
public class AppMessageController extends CoreController {

	
	/**
	 * 默认方法查询数据
	 */
	public void index(){
		
		//获取查询条件
		AppMessage appMessage = getModel(AppMessage.class);
		AppBasedoc appBasedoc = getModel(AppBasedoc.class);
		
		setAttr("appBasedoc", appBasedoc);
		setAttr("appMessage", appMessage);
		
		
		//组装sql
		String selectSql = " select a.*,b.vcode vcode_basedoc,b.vname vname_basedoc ";
		StringBuffer strfromSql = new StringBuffer(" from app_message a, app_basedoc b where a.pk_basedoc=b.pk_basedoc and a.dr=0");
		
		if(appMessage.getStr(appMessage.VCODE).equals("")==false){
			strfromSql.append(" and a.vcode like '%"+appMessage.getStr(appMessage.VCODE)+"%' ");
		}
		if(appMessage.getStr(appMessage.VNAME).equals("")==false){
			strfromSql.append(" and a.vname like '%"+appMessage.getStr(appMessage.VNAME)+"%' ");
		}
		
		//执行查询
		if(appBasedoc.getPrimaryValue().equals("")==false){
			strfromSql.append(" and b.pk_basedoc=? ");
			setAttr("page",appMessage.paginate(getPageNumber(), getPageSize(), selectSql, strfromSql.toString(), new Object[]{appBasedoc.getPrimaryValue()})); //分页查询
		}else{
			setAttr("page",appMessage.paginate(getPageNumber(), getPageSize(), selectSql, strfromSql.toString())); //分页查询
		}
		
		renderJsp("/WEB-INF/jsp/app/server/s04/app_message_list.jsp");
	}

	/**
	 * 保存数据
	 */
	public void saveModel(){
		try {
			AppMessage appMessage= getModel(AppMessage.class);
			String pkBasedoc = getModel(AppBasedoc.class).getPrimaryValue();
			appMessage.set(AppMessage.TIMES,"0");
			if(appMessage.set(appMessage.PK_BASEDOC, pkBasedoc).save()){
				DwzRender dwz = new DwzRender();
				dwz.message("操作成功");
				dwz.callbackType("closeCurrent");
				dwz.navTabId("app_message_list");
				render(dwz);
			}else{
				render(DwzRender.error());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			render(DwzRender.error());
		}
		
	}
	
	/**
	 * 转入新增页面
	 */
	public void addPage(){
		renderJsp("/WEB-INF/jsp/app/server/s04/app_message_form.jsp");
	}
	
	/**
	 * 转入修改页面
	 */
	public void editPage(){
		AppMessage appMessage = getModel(AppMessage.class).queryById();
		
		setAttr("appBasedoc", getModel(AppBasedoc.class).setPrimaryValue(appMessage.getStr(appMessage.PK_BASEDOC)).queryById());
		
		setAttr("appMessage", appMessage);
		renderJsp("/WEB-INF/jsp/app/server/s04/app_message_form.jsp");
	}
	
	/**
	 * 删除数据
	 */
	public void delModel(){
		getModel(AppMessage.class).delete();
		render(DwzRender.success());
	}
	
	/**
	 * 推送数据
	 */
	public void sendMessage(){
		try {
			AppMessage appMessage  = getModel(AppMessage.class).queryById();
			appMessage.set(AppMessage.TIMES, appMessage.getInt(AppMessage.TIMES)+1);
			
			AppBasedoc appBasedoc = new AppBasedoc().set(AppBasedoc.PK_BASEDOC, appMessage.getStr(appMessage.PK_BASEDOC)).queryById();
			
			//发送消息
			boolean isSend = MessageServer.sendMessage(appBasedoc.getStr(appBasedoc.VCODE), 
					appMessage.getStr(appMessage.VNAME), appMessage.getStr(appMessage.COMMENTS));
			
			//设置消息
			if(isSend){
				appMessage.set(appMessage.STATUS, 1);
			}else{
				appMessage.set(appMessage.STATUS, 2);
			}
			
			//保存数据库
			if(appMessage.save()&& isSend){
				DwzRender dwz = new DwzRender();
				dwz.message("操作成功");
				dwz.navTabId("app_message_list");
				render(dwz);
			}else{
				render(DwzRender.error("推送失败,可能没有客户端在线!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logError("操作失败,请联系管理员!");
			render(DwzRender.error());
		}
	}
	
	
	public void sendMessageTest(){
		try{
			String pk_message = getModel(AppMessage.class).getPrimaryValue();
			AppMessage appMessage = getModel(AppMessage.class).query(AppMessage.PK_MESSAGE, pk_message).get(0); 
			String appCode = (String)appMessage.get(AppMessage.VCODE);
			String vname = (String)appMessage.get(AppMessage.VNAME);
			String msg = (String)appMessage.get(AppMessage.COMMENTS);
			boolean isSendOk = MessageServer.sendMessage(appCode, vname, msg);
			if(isSendOk == true){
				
			}
		}catch(Exception e){
			e.printStackTrace();
			logError("推送失败");
		}
	}
}
